package com.log.service.Impl;

import com.log.common.util.PageHelper;
import com.log.common.vo.ResStatus;
import com.log.common.vo.ResultVo;
import com.log.dao.LogDepartmentMapper;
import com.log.pojo.LogDepartment;
import com.log.service.LogDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

@Service
public class LogDepartmentServiceImpl implements LogDepartmentService {
    @Autowired
    private LogDepartmentMapper logDepartmentMapper;


    @Override
    //通过id查询部门信息
    public ResultVo getLoglogDepartmentById(int id) {
        LogDepartment logDepartment = logDepartmentMapper.selectByPrimaryKey(id);
        return new ResultVo(ResStatus.OK,"sucess",logDepartment);
    }

    @Override
    //通过id删除部门信息
    public ResultVo deleteLogDepartmentById(int id) {
        int logDepartment =logDepartmentMapper.deleteByPrimaryKey(id);
        return new ResultVo(ResStatus.OK,"删除成功",null);
    }

    @Override
    //增加部门信息
    public ResultVo insertLogDepartment(LogDepartment logDepartment) {
        Date date1 = new Date();
        logDepartment.setLogCreatedTime(date1);
        Example example = new Example(LogDepartment.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("logDepartmentName",logDepartment.getLogDepartmentName());
        List<LogDepartment> logDepartments = logDepartmentMapper.selectByExample(example);
        if(logDepartments.size()>0){
            return new ResultVo(ResStatus.NO,"该部门名已存在",null);
        }else{
            logDepartmentMapper.insert(logDepartment);
            return new ResultVo(ResStatus.OK,"部门增加成功",null);
        }

    }

    @Override
    //修改部门信息
    public ResultVo updateLogDepartment(LogDepartment logDepartment) {
        Date date1 =new Date();
        logDepartment.setLogCreatedTime(date1);
            logDepartmentMapper.updateByPrimaryKeySelective(logDepartment);
            return new ResultVo(ResStatus.OK,"修改成功",logDepartment);
        }

    @Override
    public ResultVo selectLogDepartmentListByPage(int pageNum, int limit) {

        //1.起始索引
        int start = (pageNum-1)*limit;
        //2.查询结果
        List<LogDepartment> list = logDepartmentMapper.selectLogDepartmentListByPage(start,limit);
        //3查询总数
        tk.mybatis.mapper.entity.Example example = new tk.mybatis.mapper.entity.Example(LogDepartment.class);
        Example.Criteria criteria = example.createCriteria();
        int count = logDepartmentMapper.selectCountByExample(example);
        //4计算总页数
        int pageCount = count%limit==0 ? count/limit:count/limit+1;
        return new ResultVo(ResStatus.OK,"sucess",new PageHelper<LogDepartment>(count,pageCount,list));

    }

    @Override
    public ResultVo getAll() {
        List<LogDepartment> logDepartments = logDepartmentMapper.selectAll();
        return new ResultVo(ResStatus.OK,"sucess",logDepartments);
    }

    @Override
    public ResultVo selectLogDepartmentList() {
        List<LogDepartment> logDepartments = logDepartmentMapper.selectAll();
        return new ResultVo(ResStatus.OK,"success",logDepartments);
    }

}



